package com.ys.nms.module.business.service.alerts;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ys.nms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.ys.nms.framework.tenant.core.aop.TenantIgnore;
import com.ys.nms.module.business.dal.dataobject.companyproperty.CompanyPropertyDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ys.nms.module.business.controller.admin.alerts.vo.*;
import com.ys.nms.module.business.dal.dataobject.alerts.AlertsDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.alerts.AlertsMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * 报警 Service 实现类
 *
 * @author zzy
 */
@Service
@Validated
public class AlertsServiceImpl implements AlertsService {

    @Resource
    private AlertsMapper alertsMapper;

    @Override
    public Integer createAlerts(AlertsSaveReqVO createReqVO) {
        // 插入
        AlertsDO alerts = BeanUtils.toBean(createReqVO, AlertsDO.class);
        alertsMapper.insert(alerts);

        // 返回
        return alerts.getId();
    }

    @Override
    public void updateAlerts(AlertsSaveReqVO updateReqVO) {
        // 校验存在
        validateAlertsExists(updateReqVO.getId());
        // 更新
        AlertsDO updateObj = BeanUtils.toBean(updateReqVO, AlertsDO.class);
        alertsMapper.updateById(updateObj);
    }

    @Override
    public void deleteAlerts(Integer id) {
        // 校验存在
        validateAlertsExists(id);
        // 删除
        alertsMapper.deleteById(id);
    }

    @Override
        public void deleteAlertsListByIds(List<Integer> ids) {
        // 删除
        alertsMapper.deleteByIds(ids);
        }


    private void validateAlertsExists(Integer id) {
        if (alertsMapper.selectById(id) == null) {
            throw exception(ALERTS_NOT_EXISTS);
        }
    }

    @Override
    public AlertsDO getAlerts(Integer id) {
        return alertsMapper.selectById(id);
    }

    @Override
    public PageResult<AlertsDO> getAlertsPage(AlertsPageReqVO pageReqVO) {
        return alertsMapper.selectPage(pageReqVO);
    }

    @Override
    @TenantIgnore
    @Slave
    public List<AlertsDO> getAlertsListByDeviceId(AlertsPageReqVO pageReqVO) {
        LambdaQueryWrapperX<AlertsDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(AlertsDO::getDeviceId, pageReqVO.getDeviceId());
        queryWrapper.eqIfPresent(AlertsDO::getOpen, pageReqVO.getOpen());
        queryWrapper.eqIfPresent(AlertsDO::getDeviceId, pageReqVO.getDeviceId());
        queryWrapper.eqIfPresent(AlertsDO::getRuleId, pageReqVO.getRuleId());
        queryWrapper.eqIfPresent(AlertsDO::getState, pageReqVO.getState());
        queryWrapper.eqIfPresent(AlertsDO::getAlerted, pageReqVO.getAlerted());
        queryWrapper.eqIfPresent(AlertsDO::getOpen, pageReqVO.getOpen());
        queryWrapper.eqIfPresent(AlertsDO::getNote, pageReqVO.getNote());
        queryWrapper.betweenIfPresent(AlertsDO::getTimestamp, pageReqVO.getTimestamp());
        queryWrapper.orderByDesc(AlertsDO::getTimestamp);
        return alertsMapper.selectList(queryWrapper);
    }

}